export function fileRead<T extends Blob>(blob: T, type: 'base64'): Promise<string | null>
export function fileRead<T extends Blob>(blob: T, type: 'buffer'): Promise<ArrayBuffer | null>
export function fileRead<T extends Blob>(blob: T, type: 'text', encoding?: string): Promise<string | null>
export function fileRead<T extends Blob>(
  blob: T,
  type: 'text' | 'base64' | 'buffer',
  encoding?: string
): Promise<string | ArrayBuffer | null> {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    // 返回错误信息
    reader.addEventListener('error', function () {
      reject(new Error('FileReader 读取资源失败'))
    })
    // 读取成功，返回读取到的资源
    reader.addEventListener('load', function () {
      resolve(reader.result)
    })
    // 不同的读取方式
    switch (type) {
      case 'text':
        reader.readAsText(blob, encoding)
        break
      case 'base64':
        reader.readAsDataURL(blob)
        break
      // case 'buffer':
      default:
        reader.readAsArrayBuffer(blob)
        break
    }
  })
}

export const MIME_CODE_TABLE = [
  { postfix: '.aac', mime: 'audio/aac' },
  { postfix: '.abw', mime: 'application/x-abiword' },
  { postfix: '.arc', mime: 'application/x-freearc' },
  { postfix: '.avi', mime: 'video/x-msvideo' },
  { postfix: '.azw', mime: 'application/vnd.amazon.ebook' },
  { postfix: '.bin', mime: 'application/octet-stream' },
  { postfix: '.bmp', mime: 'image/bmp' },
  { postfix: '.bz', mime: 'application/x-bzip' },
  { postfix: '.bz2', mime: 'application/x-bzip2' },
  { postfix: '.csh', mime: 'application/x-csh' },
  { postfix: '.css', mime: 'text/css' },
  { postfix: '.csv', mime: 'text/csv' },
  { postfix: '.doc', mime: 'application/msword' },
  { postfix: '.docx', mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' },
  { postfix: '.eot', mime: 'application/vnd.ms-fontobject' },
  { postfix: '.epub', mime: 'application/epub+zip' },
  { postfix: '.gif', mime: 'image/gif' },
  { postfix: '.htm', mime: 'text/html' },
  { postfix: '.html', mime: 'text/html' },
  { postfix: '.ico', mime: 'image/vnd.microsoft.icon' },
  { postfix: '.ics', mime: 'text/calendar' },
  { postfix: '.jar', mime: 'application/java-archive' },
  { postfix: '.jpeg', mime: 'image/jpeg' },
  { postfix: '.jpg', mime: 'image/jpeg' },
  { postfix: '.js', mime: 'text/javascript' },
  { postfix: '.json', mime: 'application/json' },
  { postfix: '.jsonld', mime: 'application/ld+json' },
  { postfix: '.mid', mime: 'audio/midi' },
  { postfix: '.mid', mime: 'audio/x-midi' },
  { postfix: '.midi', mime: 'audio/midi' },
  { postfix: '.midi', mime: 'audio/x-midi' },
  { postfix: '.mjs', mime: 'text/javascript' },
  { postfix: '.mp3', mime: 'audio/mpeg' },
  { postfix: '.mpeg', mime: 'video/mpeg' },
  { postfix: '.mpkg', mime: 'application/vnd.apple.installer+xml' },
  { postfix: '.odp', mime: 'application/vnd.oasis.opendocument.presentation' },
  { postfix: '.ods', mime: 'application/vnd.oasis.opendocument.spreadsheet' },
  { postfix: '.odt', mime: 'application/vnd.oasis.opendocument.text' },
  { postfix: '.oga', mime: 'audio/ogg' },
  { postfix: '.ogv', mime: 'video/ogg' },
  { postfix: '.ogx', mime: 'application/ogg' },
  { postfix: '.otf', mime: 'font/otf' },
  { postfix: '.png', mime: 'image/png' },
  { postfix: '.pdf', mime: 'application/pdf' },
  { postfix: '.ppt', mime: 'application/vnd.ms-powerpoint' },
  { postfix: '.pptx', mime: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' },
  { postfix: '.rar', mime: 'application/x-rar-compressed' },
  { postfix: '.rtf', mime: 'application/rtf' },
  { postfix: '.sh', mime: 'application/x-sh' },
  { postfix: '.svg', mime: 'image/svg+xml' },
  { postfix: '.swf', mime: 'application/x-shockwave-flash' },
  { postfix: '.tar', mime: 'application/x-tar' },
  { postfix: '.tif', mime: 'image/tiff' },
  { postfix: '.tiff', mime: 'image/tiff' },
  { postfix: '.ttf', mime: 'font/ttf' },
  { postfix: '.txt', mime: 'text/plain' },
  { postfix: '.vsd', mime: 'application/vnd.visio' },
  { postfix: '.wav', mime: 'audio/wav' },
  { postfix: '.weba', mime: 'audio/webm' },
  { postfix: '.webm', mime: 'video/webm' },
  { postfix: '.webp', mime: 'image/webp' },
  { postfix: '.woff', mime: 'font/woff' },
  { postfix: '.woff2', mime: 'font/woff2' },
  { postfix: '.xhtml', mime: 'application/xhtml+xml' },
  { postfix: '.xls', mime: 'application/vnd.ms-excel' },
  { postfix: '.xlsx', mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' },
  { postfix: '.xml', mime: 'application/xml' },
  { postfix: '.xml', mime: 'text/xml' },
  { postfix: '.xul', mime: 'application/vnd.mozilla.xul+xml' },
  { postfix: '.zip', mime: 'application/zip' },
  { postfix: '.3gp', mime: 'video/3gpp' },
  { postfix: '.3gp', mime: 'audio/3gpp' },
  { postfix: '.3g2', mime: 'video/3gpp2' },
  { postfix: '.3g2', mime: 'audio/3gpp2' },
  { postfix: '.7z', mime: 'application/x-7z-compressed' },
  { postfix: '.mp4', mime: 'video/mp4' },
]

export function normalizeFileName(blob: Blob, name: string): string {
  const { type } = blob
  const match = type ? MIME_CODE_TABLE.find(({ mime }) => mime === type) : null
  if (match) {
    return name.toLowerCase().endsWith(match.postfix) ? name : `${name}${match.postfix}`
  }
  return name
}
